#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "function.h"

#pragma execution_character_set("utf-8")

#include <QMessageBox>
#include <QFileDialog>
#include <QCloseEvent>
#include <QDebug>
#include <QTime>
#include <QDir>
#include <QFile>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QInputDialog>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    globalMark=0;  
    isDrawing=false;//初始化
    ui->stackedWidget->setCurrentIndex(0);

}

MainWindow::~MainWindow()
{
    delete ui;
}

//打开图片
void MainWindow::on_action_open_triggered()
{
    //判断展示图片的框里是否有图片，如果有图片判断是否保存，若图片未保存不能打开新图片
    if(globalMark == 1) {
        if(proImage.getLastImage() != proImage.getFinalImage()) {
            QMessageBox mes;
            mes.warning(this,"提示信息","当前图片未保存，请先保存再打开新的图片!");
            return;
        }
        else globalMark = 0;
    }
     //打开图片
    QString fileName = QFileDialog::getOpenFileName(
                this, tr("open image file"),
                "./", tr("Image files(*.bmp);;All files (*.*)"));

    if(fileName.isEmpty())
    {
        QMessageBox mes;
        mes.warning(this,"提示信息","打开图片失败!");
        return;
    }
    else
    {
        QImage image(fileName);
        //清除展示图片的框
        ui->show_label->clear();
        //将打开的图片放在显示框的中间位置
        ui->show_label->setPixmap(QPixmap::fromImage(image));
        ui->show_label->setAlignment(Qt::AlignCenter);
        ui->show_label->show();

        if(image.isNull())
        {
            QMessageBox::information(this,"提示信息","打开文件失败");
        }
        else
        {
            globalMark=1;//存储标记，用于判断是否有文件
            globalImage=image;
            proImage.copyPicture(image);
        }
    }

}

//保存图片,保存之后无法撤销
void MainWindow::on_action_save_triggered()
{
    if(globalMark == 1)////如果标记为1，证明有图片正在被编辑，不然没有则不需要保存
    {
        QString filePath;//存储保存路径
        QString saveName="保存实验图";//保存图片名字
        QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
        QString str = time.toString("yyyyMMdd_hhmmss"); //设置显示格式
        saveName += str;
        filePath = QFileDialog::getExistingDirectory(this,"");//获取文件夹路径
        if(filePath.isEmpty())
        {
            QMessageBox::information(this,"提示信息","保存失败");
        }
        else
        {
            QImage images;
            images=globalImage;//获取修改的图片

            //将图片重新命名并保存至文件夹里
            QString savePath=QString("%1//%2.bmp").arg(filePath).arg(saveName);
            images.save(savePath);//QImage保存方法
            QMessageBox::information(this,"提示信息","保存成功");
            proImage.setLastImage(proImage.getFinalImage());
        }
    }
    else
    {
        QMessageBox::information(this,"提示信息","保存失败");
    }
}


void MainWindow::on_action_back_triggered()
{
    if(proImage.getLastImage() == proImage.getFinalImage())
        return;
    proImage.setFinalImage(proImage.getLastImage());
    ui->show_label->clear();
    ui->show_label->setPixmap(QPixmap::fromImage(proImage.getFinalImage()));
}

void MainWindow::on_action_close_triggered()
{
    //判断展示图片的框里是否有图片，如果有图片判断是否保存，若图片未保存不能关闭
    if(globalMark == 1) {
        if(proImage.getLastImage() != proImage.getFinalImage()) {
            QMessageBox::StandardButton result=QMessageBox::question(this, "提示信息", "当前图片未保存，确定要继续退出吗？",
                                  QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel);
                if (result==QMessageBox::Yes)
                    qApp->quit();
                else
                    return;
        }
    }
    QMessageBox::StandardButton result=QMessageBox::question(this, "提示信息", "确定要退出吗？",
                          QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel);
    if (result==QMessageBox::Yes)
        qApp->quit();
    else
        return;
}

void MainWindow::on_action_expand_triggered()
{
    ui->stackedWidget->setCurrentIndex(3);
}

void MainWindow::on_action_shrink_triggered()
{
    ui->stackedWidget->setCurrentIndex(4);
}

void MainWindow::on_action_revolve_triggered()
{
    ui->stackedWidget->setCurrentIndex(5);
}

void MainWindow::on_action_square_triggered()
{
    QImage image = proImage.squarePicture();
    ui->show_label->clear();
    ui->show_label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::on_action_mid_triggered()
{
    QImage image = proImage.midPicture();
    ui->show_label->clear();
    ui->show_label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::on_action_gaosi_triggered()
{
    QImage image = proImage.gaosiPicture();
    ui->show_label->clear();
    ui->show_label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::on_action_average_triggered()
{
    QImage image = proImage.averagePicture();
    ui->show_label->clear();
    ui->show_label->setPixmap(QPixmap::fromImage(image));
}


void MainWindow::on_action_text_triggered()
{
    ui->stackedWidget->setCurrentIndex(2);
}


void MainWindow::getContent(int decide, int page) {
    if(decide == 1) {
        switch(page) {

        case 2:
            ui->textEdit_2->clear();
            break;
        case 3:
            ui->textEdit_3->clear();
            break;
        case 4:
            ui->textEdit_4->clear();
            break;
        case 5:
            ui->textEdit_5->clear();
            break;

        }
    }
    else {
        QString str;
        double num;
        QImage image = proImage.getFinalImage();
        int width = proImage.getFinalImage().width();
        int height = proImage.getFinalImage().height();
        QPainter painter(&image);
        switch(page) {

        case 2:
            str = ui->textEdit_2->toPlainText();
            painter.setPen(QColor(0,0,0));
            painter.setFont(QFont(str,sizeof(str)));
            painter.drawText(image.rect(), str);
            ui->show_label->setPixmap(QPixmap::fromImage(image));
            break;
        case 3:
            str = ui->textEdit_3->toPlainText();
            num = str.toDouble();
            image = proImage.changedPictureSize(num);
            ui->show_label->clear();
            ui->show_label->setPixmap(QPixmap::fromImage(image));
            break;
        case 4:
            str = ui->textEdit_4->toPlainText();
            num = str.toDouble();
            image = proImage.changedPictureSize(num);
            ui->show_label->clear();
            ui->show_label->setPixmap(QPixmap::fromImage(image));
            break;
        case 5:
            str = ui->textEdit_5->toPlainText();
            num = str.toDouble();
            QImage image = proImage.revolvePicture(num);
            ui->show_label->clear();
            ui->show_label->setPixmap(QPixmap::fromImage(image));
            break;
        }
    }
}

void MainWindow::on_pushButton_delete_5_clicked()
{
    getContent(1, 5);
}

void MainWindow::on_pushButton_confirm_5_clicked()
{
    getContent(2, 5);
}

void MainWindow::on_pushButton_delete_4_clicked()
{
    getContent(1, 4);
}

void MainWindow::on_pushButton_confirm_4_clicked()
{
    getContent(2, 4);
}

void MainWindow::on_pushButton_delete_3_clicked()
{
    getContent(1, 3);
}

void MainWindow::on_pushButton_confirm_3_clicked()
{
    getContent(2, 3);
}

void MainWindow::on_pushButton_delete_2_clicked()
{
    getContent(1, 2);
}

void MainWindow::on_pushButton_confirm_2_clicked()
{
    getContent(2, 2);
}
